<?php
namespace app\api\controller;

use app\api\model\ManualCards;
use app\api\model\Wallets;
use Illuminate\Support\Carbon;
use support\Request;


class ManualCardsController
{
    /**
     * @param Request $request
     * @param ManualCards $DB
     * @return \support\Response
     * 添加母鸡卡模板
     */
    public function add(Request $request, ManualCards $DB,Wallets $Wallets)
    {
        $req = $request->only(['issuer', 'consumer', 'green_card_1', 'green_card_2', 'green_card_3', 'green_card_4',
            'green_card_5', 'blue_card_1', 'blue_card_2','gold_card_1','red_card_1','black_card_1','randomization']);
        $req['created_at'] = Carbon::now()->toDateTimeString();
        //添加发卡记录
        $DB->insertGetId($req);
        $wallet_db  = $Wallets->where('wallet_user_id', $req['consumer'])->first();
        $new_count = [
            'green_card_1'=>$req['green_card_1']+$wallet_db['green_card_1'],
            'green_card_2'=>$req['green_card_2']+$wallet_db['green_card_2'],
            'green_card_3'=>$req['green_card_3']+$wallet_db['green_card_3'],
            'green_card_4'=>$req['green_card_4']+$wallet_db['green_card_4'],
            'green_card_5'=>$req['green_card_5']+$wallet_db['green_card_5'],
            'blue_card_1'=>$req['blue_card_1']+$wallet_db['blue_card_1'],
            'blue_card_2'=>$req['blue_card_2']+$wallet_db['blue_card_2'],
            'gold_card_1'=>$req['gold_card_1']+$wallet_db['gold_card_1'],
            'red_card_1'=>$req['red_card_1']+$wallet_db['red_card_1'],
            'black_card_1'=>$req['black_card_1']+$wallet_db['black_card_1'],
        ];
        //更新钱包数据
        $res = $Wallets->where('wallet_user_id', $req['consumer'])->update($new_count);
        $code = 2;
        $msg = '添加成功!';
        return json([
            'code' => $code,
            'msg' => $msg,
            'data' => $res,
        ]);
    }

  
    /**
     * @param Request $request
     * @param User $DB
     * @param consumer 收卡人user_id
     * @return \support\Response
     * 获取发卡信息列表
     */
    function list(Request $request, ManualCards $DB)
    {
        $consumer = $request->get('consumer', '');
        $res = $DB->where('consumer', 'like', '%' . $consumer . '%')->orderBy('id', 'asc')
            ->paginate(10);
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }


}
